home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mmdf / mmdf-IIb.43 / uip / ucbmail / strings.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-06-03  |  2.1 KB  |  95 lines

  1. /*
  2.  * Copyright (c) 1980 Regents of the University of California.
  3.  * All rights reserved.  The Berkeley software License Agreement
  4.  * specifies the terms and conditions for redistribution.
  5.  */
  6.  
  7. #ifndef lint
  8. static char *sccsid = "@(#)strings.c    5.2 (Berkeley) 6/21/85";
  9. #endif not lint
  10.  
  11. /*
  12.  * Mail -- a mail program
  13.  *
  14.  * String allocation routines.
  15.  * Strings handed out here are reclaimed at the top of the command
  16.  * loop each time, so they need not be freed.
  17.  */
  18.  
  19. #include "./rcv.h"
  20.  
  21. /*
  22.  * Allocate size more bytes of space and return the address of the
  23.  * first byte to the caller.  An even number of bytes are always
  24.  * allocated so that the space will always be on a word boundary.
  25.  * The string spaces are of exponentially increasing size, to satisfy
  26.  * the occasional user with enormous string size requests.
  27.  */
  28.  
  29. char *
  30. salloc(size)
  31. {
  32.     register char *t;
  33.     register int s;
  34.     register struct strings *sp;
  35.     int ind;
  36.  
  37.     /*
  38.      * calculate actual size required by incrementing s until it
  39.      * reaches a size that is a multiple of the required alignment.
  40.      */
  41.     s = size;
  42.     while (s & (ALIGNMENT - 1))
  43.         s++;
  44.  
  45.     ind = 0;
  46.     for (sp = &stringdope[0]; sp < &stringdope[NSPACE]; sp++) {
  47.         if (sp->s_topFree == NOSTR && (STRINGSIZE << ind) >= s)
  48.             break;
  49.         if (sp->s_nleft >= s)
  50.             break;
  51.         ind++;
  52.     }
  53.     if (sp >= &stringdope[NSPACE])
  54.         panic("String too large");
  55.     if (sp->s_topFree == NOSTR) {
  56.         ind = sp - &stringdope[0];
  57.         sp->s_topFree = (char *) calloc(STRINGSIZE << ind,
  58.             (unsigned) 1);
  59.         if (sp->s_topFree == NOSTR) {
  60.             fprintf(stderr, "No room for space %d\n", index);
  61.             panic("Internal error");
  62.         }
  63.         sp->s_nextFree = sp->s_topFree;
  64.         sp->s_nleft = STRINGSIZE << ind;
  65.     }
  66.     sp->s_nleft -= s;
  67.     t = sp->s_nextFree;
  68.     sp->s_nextFree += s;
  69.     return(t);
  70. }
  71.  
  72. /*
  73.  * Reset the string area to be empty.
  74.  * Called to free all strings allocated
  75.  * since last reset.
  76.  */
  77.  
  78. sreset()
  79. {
  80.     register struct strings *sp;
  81.     register int ind;
  82.  
  83.     if (noreset)
  84.         return;
  85.     minit();
  86.     ind = 0;
  87.     for (sp = &stringdope[0]; sp < &stringdope[NSPACE]; sp++) {
  88.         if (sp->s_topFree == NOSTR)
  89.             continue;
  90.         sp->s_nextFree = sp->s_topFree;
  91.         sp->s_nleft = STRINGSIZE << ind;
  92.         ind++;
  93.     }
  94. }
  95.